package com.boss.controller;

import com.boss.pojo.Result;
import com.boss.pojo.User;
import com.boss.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    //注册
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        log.info("用户注册,用户名:{},密码:{},姓名:{},邮箱:{},手机号:{},身份：{}",user.getUserName(),user.getPassword(),
                user.getName(),user.getEmail(),user.getPhone(),user.getRole());
        String username = user.getUserName();
        //查询用户是否存在
        if (userService.findByUsername(username)!=null) {
            //用户已存在
            return Result.error("用户名已存在");
        }
        //查询用户的身份是否为管理员
        if (user.getRole()==2) {
            return Result.error("不能以管理员身份注册");
        }
        //注册
        userService.register(user);
        return Result.success();
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        log.info("员工登录：{}", user);
        User u = userService.login(user);

        //登录成功，生成令牌，下发令牌
        if (u != null) {
 /*           Map<String, Object> claims = new HashMap<>();
            claims.put("id", u.getId());
            claims.put("username", u.getUsername());
            claims.put("password", u.getPassword());
            claims.put("identity",u.getIdentity());
            claims.put("email", u.getEmail());
            claims.put("phone", u.getPhone());

            String jwt = JwtUtils.generateJwt(claims);//*/
            return Result.success(u);
        }
        //登录失败，返回错误信息
        return Result.error("用户名或密码错误");
    }



}
